﻿'' GoogleCode - http://code.google.com/p/outlook-4-you/
'' GNU GPL v3 Licencing

Imports System.Data.OleDb
Imports System.Data
Imports System.DirectoryServices
Imports System.Windows.Forms
Imports System.Threading

Public Class frmRequest

#Region "Declarations"
#Region "Form declarations"
	Private Shared FormExists As Boolean = False
	Public Shared ReadOnly Property InstanceExists() As Boolean
		Get
            Return frmRequest.FormExists
        End Get
    End Property
    Public Sub New(ByVal sBuild As String)
        InitializeComponent()
        If sBuild <> "" Then
            PopulateRequest(sBuild)
        End If
    End Sub
#End Region

#Region "IO declarations"
    Public root As String = "C:\build"
    Public release As String = root & "\release.csv"
    Public builds As String = root & "\builder.csv"
    Public rel163 As String = root & "\rel163.csv"
    Public rel170 As String = root & "\rel170.csv"
    Public rel171 As String = root & "\rel171.csv"
    Public rel172 As String = root & "\rel172.csv"
    Public rel180 As String = root & "\rel180.csv"
#End Region

#Region "DB declarations"
    Public Const buildtable As String = "Builds"
    Public Const connString As String = "A"
#End Region

#Region "Inter-declarations"
    Public Shared catalogue()() As String
    Public Shared component As String
    Public Shared builder As String
    Public Shared buildmail As String
#End Region
#End Region

#Region "File Operations"
    Private Function Read_2D(ByVal path As String, ByVal cnt As Integer) As String()()
        Dim csv As New CSV(path)
        Return csv.Read(cnt)
    End Function ' Puts sourcefile catalogue into array
    Public Function Write_2D() As Boolean
        Dim rel As String = cboRel.SelectedItem.ToString
        Dim central_housing As String = ""
        Select Case rel
            Case "16.3"
                central_housing = rel163
            Case "17.0"
                central_housing = rel170
            Case "17.1"
                central_housing = rel171
            Case "17.2"
                central_housing = rel172
            Case "18.0"
                central_housing = rel180
            Case "18.1"
                'central_housing = rel181
        End Select
        Dim csv As New CSV(central_housing)
        If csv.Write(central_housing) = True Then
            Return True
        End If
        Return False
    End Function
    Private Function popProduct() As Boolean
        Dim prod As String()()
        Try
            prod = Read_2D(release, 2)
        Catch ex As Exception
            MsgBox("Failed to retrieve Product Information")
            Return False
        End Try

        For i = 0 To prod.Length - 1
            If cboProd.Items.Contains(prod(i)(0)) = True Then
                'MsgBox(prod(i)(0) & "already in list")
            Else
                cboProd.Items.Add(prod(i)(0).ToString)
            End If
        Next
        Return True
    End Function ' Populate Product combobox
    Private Function popRelease() As Boolean
        cboRel.Items.Clear()
        Dim rel As String()()
        Try
            rel = Read_2D(release, 2)
        Catch ex As Exception
            MsgBox("Failed to retrieve Release Information")
            Return False
        End Try

        For i = 0 To rel.Length - 1
            If cboProd.Text.Contains(rel(i)(0)) = True Then
                cboRel.Items.Add(rel(i)(1).ToString)
            Else
                'MsgBox("Product information not available")
            End If
        Next
        Return True
    End Function ' Populate Release combobox
    Private Function popRelArray(ByVal rel As String) As Boolean
        Select Case rel
            Case "16.3"
                Try
                    catalogue = Read_2D(rel163, 4)
                    Return True
                Catch ex As Exception
                    MsgBox("Failed to retrieve Source code catalogue")
                    Return False
                End Try
            Case "17.0"
                Try
                    catalogue = Read_2D(rel170, 4)
                    Return True
                Catch ex As Exception
                    MsgBox("Failed to retrieve Source code catalogue")
                    Return False
                End Try
            Case "17.1"
                Try
                    catalogue = Read_2D(rel171, 4)
                    Return True
                Catch ex As Exception
                    MsgBox("Failed to retrieve Source code catalogue")
                    Return False
                End Try
            Case "17.2"
                Try
                    catalogue = Read_2D(rel172, 4)
                    Return True
                Catch ex As Exception
                    MsgBox("Failed to retrieve Source code catalogue")
                    Return False
                End Try
            Case "18.0"
                Try
                    catalogue = Read_2D(rel180, 4)
                    Return True
                Catch ex As Exception
                    MsgBox("Failed to retrieve Source code catalogue")
                    Return False
                End Try
        End Select
    End Function
#End Region

#Region "UI events"
    Private Sub btnSVN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSVN.Click
        Dim slash As Integer = txtSVN.Text.LastIndexOf("/")
        Dim shrt As String = txtSVN.Text.Substring(slash)
        Dim lng As String = txtSVN.Text

        If lvSVN.Items.Count > 0 Then
            Dim chk As Windows.Forms.ListViewItem = lvSVN.FindItemWithText(lng, False, 0, False)
            If chk IsNot Nothing Then
                chk.Text = txtSVN.Text
                chk.SubItems(1).Text = txtSVNrev.Text
                Check_Component(shrt)
                Exit Sub
            Else
                Dim slsv As String() = New String((2) - 1) {}
                slsv(0) = txtSVN.Text
                slsv(1) = txtSVNrev.Text
                Dim lsv As ListViewItem = New ListViewItem(slsv)
                lvSVN.Items.Add(lsv)
                Check_Component(shrt)
            End If
        Else
            Dim slsv As String() = New String((2) - 1) {}
            slsv(0) = txtSVN.Text
            slsv(1) = txtSVNrev.Text
            Dim lsv As ListViewItem = New ListViewItem(slsv)
            lvSVN.Items.Add(lsv)
            Check_Component(shrt)
        End If
    End Sub ' Updates listview with source file paths
    Private Sub Check_Component(ByVal file As String)
        Dim comp As String() = New String(1) {}
        For i = 0 To catalogue.Length - 1
            If catalogue(i)(1).Contains(file) = True Then       ' File is in csv
                If catalogue(i)(2).ToString = "0" Then          ' Component not added to csv yet
                    If Add_Component(catalogue(i)(0).ToString) = False Then     ' Call adding form
                        MsgBox("Failed to add component to CSV")
                    End If
                End If
                comp(0) = catalogue(i)(2).ToString  ' built file
                comp(1) = catalogue(i)(3).ToString  ' file type
            Else
                MsgBox("File does not exist in subversion catalogue" & vbCrLf & "Amending Reuquest")
                If txtAddInfo.Text = "None" Then
                    txtAddInfo.Text = "The file(s) below must be added to the catalogue and built:" & vbCrLf & _
                                    txtSVN.Text & "::" & txtSVNrev.Text
                End If
            End If
        Next
        If lvComponents.Items.Count > 0 Then
            Dim chk As Windows.Forms.ListViewItem = lvComponents.FindItemWithText(comp(0).ToString, False, 0, False)
            If chk IsNot Nothing Then   '' not first, present
                Dim slsv As String() = New String((3) - 1) {}
                slsv(0) = comp(0)                           ' built file
                slsv(1) = comp(1)                           ' file type
                slsv(2) = chk.SubItems(2).Text.ToString     ' build option
                Dim lsv As ListViewItem = New ListViewItem(slsv)
                lvComponents.Items.Add(lsv)
            Else                        '' not first, not present
                Dim slsv As String() = New String((3) - 1) {}
                slsv(0) = comp(0)
                slsv(1) = comp(1)
                slsv(2) = "True"
                Dim lsv As ListViewItem = New ListViewItem(slsv)
                lvComponents.Items.Add(lsv)
            End If
        Else                            '' first, not present
            Dim slsv As String() = New String((3) - 1) {}
            slsv(0) = comp(0)
            slsv(1) = comp(1)
            slsv(2) = "True"
            Dim lsv As ListViewItem = New ListViewItem(slsv)
            lvComponents.Items.Add(lsv)
        End If
    End Sub ' Updates listview with built components
    Private Function Add_Component(ByVal comp As String) As Boolean
        Add_Component = False
        If frmComp.InstanceExists = False Then
            Dim cmp As New frmComp(comp, cboRel.SelectedItem.ToString)
            If cmp.ShowDialog = Windows.Forms.DialogResult.OK Then
                popRelArray(cboRel.SelectedItem.ToString)
                Return True
            End If
        End If
    End Function ' Adds built component information to CSV
    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        ' Grab details
        Dim prod As String = cboProd.Text.ToString
        Dim rel As String = cboRel.Text.ToString
        Dim desc As String = txtDescription.Text.ToString
        Dim srcfile As String = ""
        For i = 0 To lvSVN.Items.Count - 1
            Dim item As ListViewItem = lvSVN.Items(i)
            srcfile &= item.Text.ToString & "##" & item.SubItems(1).Text.ToString & "$$"
        Next
        Dim components As String = ""
        For i = 0 To lvComponents.Items.Count - 1
            Dim item As ListViewItem = lvComponents.Items(i)
            components &= item.SubItems(0).Text.ToString & "##" & item.SubItems(1).Text.ToString & "##" & item.SubItems(2).Text.ToString & "$$"
        Next
        Dim sql As String = txtSQL.Text.ToString
        Replace(sql, vbCr, "$$")
        Replace(sql, vbCrLf, "$$")
        Dim imp As String = txtImpact.Text.ToString
        Replace(imp, vbCr, "$$")
        Replace(imp, vbCrLf, "$$")
        Dim info As String = txtAddInfo.Text.ToString
        Replace(info, vbCr, "$$")
        Replace(info, vbCrLf, "$$")

        If prod = "" Or rel = "" Or desc = "" Or srcfile = "" Or components = "" Then
            MsgBox("Please fill in all details")
            Exit Sub
        End If

        If RequestAddIn.Send_email(buildmail.ToString, "Build Request", prod & "," & rel & "," & desc & "," & srcfile & "," & components & "," & sql & "," & imp & "," & info) = True Then
            MsgBox("Build Request submitted successfully")
            Me.Dispose()
        End If
    End Sub ' Creates XML format email and sends to builder

    Private Sub btnCpy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCpy.Click
        If txtBuild.Text <> "" Then
            ' Grab builders in order
            ' Send emails in search
            Me.Close()
        End If
    End Sub ' Retrieves old build request

#End Region

#Region "Form Events"
    Private Sub frmRequest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If popProduct() = True Then
            If Whos_building() = True Then
                Me.Text = "Build Request Creator - your builder will be " & builder.ToString
            Else
                MsgBox("Unable to check for builder")
            End If
        Else
            MsgBox("Unable to retrieve Product information")
        End If
    End Sub
    Private Function Whos_building() As Boolean
        Dim builders As String()()
        Try
            builders = Read_2D(builds, 3)
        Catch ex As Exception
            MsgBox("Unable to retrieve builder")
            Return False
        End Try
        For i = 0 To builders.Length - 1
            If builders(i)(2).ToString = "1" Then
                builder = builders(i)(0).ToString
                buildmail = builders(i)(1).ToString
            End If
        Next
        Return True
    End Function
    Private Sub cboProd_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProd.TextChanged
        popRelease()
    End Sub
    Private Sub cboRel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboRel.TextChanged
        popRelArray(cboRel.SelectedItem.ToString)
    End Sub
    Private Sub lvSVN_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvSVN.SelectedIndexChanged
        txtSVN.Text = lvSVN.SelectedItems(0).Text
        txtSVNrev.Text = lvSVN.SelectedItems(0).SubItems(1).Text
    End Sub
    Private Sub lvComponents_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvComponents.MouseClick
        If lvComponents.SelectedItems(0).SubItems(2).Text.ToString = "True" Then
            lvComponents.SelectedItems(0).SubItems(2).Text = "False"
        Else
            lvComponents.SelectedItems(0).SubItems(2).Text = "True"
        End If
    End Sub
    Private Sub txtSVN_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSVN.TextChanged
        If txtSVN.Text <> "" And txtSVNrev.Text <> "" Then
            btnSVN.Enabled = True
        Else
            btnSVN.Enabled = False
        End If
    End Sub
    Private Sub txtSVNrev_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSVNrev.KeyPress
        Select Case e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
                e.Handled = False
            Case Else
                e.Handled = True
        End Select
    End Sub
    Private Sub txtSVNrev_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSVNrev.TextChanged
        If txtSVN.Text <> "" And txtSVNrev.Text <> "" Then
            btnSVN.Enabled = True
        Else
            btnSVN.Enabled = False
        End If
    End Sub
    Private Sub PopulateRequest(ByVal build As String)

    End Sub
#End Region

End Class